home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / xdg / DesktopEntry.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  18.3 KB  |  548 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. """
  5. Complete implementation of the XDG Desktop Entry Specification Version 0.9.4
  6. http://standards.freedesktop.org/desktop-entry-spec/
  7.  
  8. Not supported:
  9. - Encoding: Legacy Mixed
  10. - Does not check exec parameters
  11. - Does not check URL's
  12. - Does not completly validate deprecated/kde items
  13. - Does not completly check categories
  14. """
  15. from xdg.IniFile import *
  16. from xdg.BaseDirectory import *
  17. import os.path as os
  18.  
  19. class DesktopEntry(IniFile):
  20.     '''Class to parse and validate DesktopEntries'''
  21.     defaultGroup = 'Desktop Entry'
  22.     
  23.     def __init__(self, filename = None):
  24.         self.content = dict()
  25.         if filename and os.path.exists(filename):
  26.             self.parse(filename)
  27.         elif filename:
  28.             self.new(filename)
  29.         
  30.  
  31.     
  32.     def __str__(self):
  33.         return self.getName()
  34.  
  35.     
  36.     def __cmp__(self, other):
  37.         return cmp(self, other)
  38.  
  39.     
  40.     def parse(self, file):
  41.         IniFile.parse(self, file, [
  42.             'Desktop Entry',
  43.             'KDE Desktop Entry'])
  44.  
  45.     
  46.     def getType(self):
  47.         return self.get('Type')
  48.  
  49.     
  50.     def getVersion(self):
  51.         return self.get('Version', type = 'numeric')
  52.  
  53.     
  54.     def getEncoding(self):
  55.         return self.get('Encoding')
  56.  
  57.     
  58.     def getName(self):
  59.         return self.get('Name', locale = True)
  60.  
  61.     
  62.     def getGenericName(self):
  63.         return self.get('GenericName', locale = True)
  64.  
  65.     
  66.     def getComment(self):
  67.         return self.get('Comment', locale = True)
  68.  
  69.     
  70.     def getNoDisplay(self):
  71.         return self.get('NoDisplay', type = 'boolean')
  72.  
  73.     
  74.     def getIcon(self):
  75.         return self.get('Icon', locale = True)
  76.  
  77.     
  78.     def getHidden(self):
  79.         return self.get('Hidden', type = 'boolean')
  80.  
  81.     
  82.     def getFilePattern(self):
  83.         return self.get('FilePattern', type = 'regex')
  84.  
  85.     
  86.     def getTryExec(self):
  87.         return self.get('TryExec')
  88.  
  89.     
  90.     def getExec(self):
  91.         return self.get('Exec')
  92.  
  93.     
  94.     def getPath(self):
  95.         return self.get('Path')
  96.  
  97.     
  98.     def getTerminal(self):
  99.         return self.get('Terminal', type = 'boolean')
  100.  
  101.     
  102.     def getSwallowTitle(self):
  103.         return self.get('SwallowTitle', locale = True)
  104.  
  105.     
  106.     def getSwallowExec(self):
  107.         return self.get('SwallowExec')
  108.  
  109.     
  110.     def getActions(self):
  111.         return self.get('Actions', list = True)
  112.  
  113.     
  114.     def getMimeType(self):
  115.         return self.get('MimeType', list = True, type = 'regex')
  116.  
  117.     
  118.     def getSortOrder(self):
  119.         return self.get('SortOrder', list = True)
  120.  
  121.     
  122.     def getDev(self):
  123.         return self.get('Dev')
  124.  
  125.     
  126.     def getFSType(self):
  127.         return self.get('FSType')
  128.  
  129.     
  130.     def getMountPoint(self):
  131.         return self.get('MountPoint')
  132.  
  133.     
  134.     def getReadonly(self):
  135.         return self.get('ReadOnly', type = 'boolean')
  136.  
  137.     
  138.     def getUnmountIcon(self):
  139.         return self.get('UnmountIcon', locale = True)
  140.  
  141.     
  142.     def getURL(self):
  143.         return self.get('URL')
  144.  
  145.     
  146.     def getCategories(self):
  147.         return self.get('Categories', list = True)
  148.  
  149.     
  150.     def getOnlyShowIn(self):
  151.         return self.get('OnlyShowIn', list = True)
  152.  
  153.     
  154.     def getNotShowIn(self):
  155.         return self.get('NotShowIn', list = True)
  156.  
  157.     
  158.     def getStartupNotify(self):
  159.         return self.get('StartupNotify', type = 'boolean')
  160.  
  161.     
  162.     def getStartupWMClass(self):
  163.         return self.get('StartupWMClass')
  164.  
  165.     
  166.     def getServiceTypes(self):
  167.         return self.get('ServiceTypes', list = True)
  168.  
  169.     
  170.     def getDocPath(self):
  171.         return self.get('DocPath')
  172.  
  173.     
  174.     def getKeywords(self):
  175.         return self.get('Keywords', list = True, locale = True)
  176.  
  177.     
  178.     def getInitialPreference(self):
  179.         return self.get('InitialPreference')
  180.  
  181.     
  182.     def getMiniIcon(self):
  183.         return self.get('MiniIcon', locale = True)
  184.  
  185.     
  186.     def getTerminalOptions(self):
  187.         return self.get('TerminalOptions')
  188.  
  189.     
  190.     def getDefaultApp(self):
  191.         return self.get('DefaultApp')
  192.  
  193.     
  194.     def getProtocols(self):
  195.         return self.get('Protocols', list = True)
  196.  
  197.     
  198.     def getExtensions(self):
  199.         return self.get('Extensions', list = True)
  200.  
  201.     
  202.     def getBinaryPattern(self):
  203.         return self.get('BinaryPattern')
  204.  
  205.     
  206.     def getMapNotify(self):
  207.         return self.get('MapNotify')
  208.  
  209.     
  210.     def new(self, filename):
  211.         if os.path.splitext(filename)[1] == '.desktop':
  212.             type = 'Application'
  213.         elif os.path.splitext(filename)[1] == '.directory':
  214.             type = 'Directory'
  215.         
  216.         self.content = dict()
  217.         self.addGroup(self.defaultGroup)
  218.         self.set('Encoding', 'UTF-8')
  219.         self.set('Type', type)
  220.         self.filename = filename
  221.  
  222.     
  223.     def checkExtras(self):
  224.         if self.defaultGroup == 'KDE Desktop Entry':
  225.             self.warnings.append('[KDE Desktop Entry]-Header is deprecated')
  226.         
  227.         if self.fileExtension == '.kdelnk':
  228.             self.warnings.append('File extension .kdelnk is deprecated')
  229.         elif self.fileExtension != '.desktop' and self.fileExtension != '.directory':
  230.             self.warnings.append('Unknown File extension')
  231.         
  232.         
  233.         try:
  234.             self.type = self.content[self.defaultGroup]['Type']
  235.         except KeyError:
  236.             self.errors.append("Key 'Type' is missing")
  237.  
  238.         
  239.         try:
  240.             self.encoding = self.content[self.defaultGroup]['Encoding']
  241.         except KeyError:
  242.             self.errors.append("Key 'Encoding' is missing")
  243.  
  244.         
  245.         try:
  246.             self.version = self.content[self.defaultGroup]['Version']
  247.         except KeyError:
  248.             self.warnings.append("Key 'Version' is missing")
  249.  
  250.         
  251.         try:
  252.             self.name = self.content[self.defaultGroup]['Name']
  253.         except KeyError:
  254.             self.errors.append("Key 'Name' is missing")
  255.  
  256.  
  257.     
  258.     def checkGroup(self, group):
  259.         if not group == self.defaultGroup and re.match('^\\Desktop Action [a-zA-Z]+\\$', group) and re.match('^\\X-', group) and group.decode('utf-8', 'ignore').encode('ascii', 'ignore') == group:
  260.             self.errors.append('Invalid Group name: %s' % group)
  261.         elif self.content[group].has_key('OnlyShowIn') and self.content[group].has_key('NotShowIn'):
  262.             self.errors.append('Group may either have OnlyShowIn or NotShowIn, but not both')
  263.         
  264.  
  265.     
  266.     def checkKey(self, key, value, group):
  267.         if key == 'Type':
  268.             if value == 'ServiceType' or value == 'Service':
  269.                 self.warnings.append('Type=%s is a KDE extension' % key)
  270.             elif value == 'MimeType':
  271.                 self.warnings.append('Type=MimeType is deprecated')
  272.             elif not value == 'Application' and value == 'Link' and value == 'FSDevice' or value == 'Directory':
  273.                 self.errors.append("Value of key 'Type' must be Application, Link, FSDevice or Directory, but is '%s'" % value)
  274.             
  275.             if self.fileExtension == '.directory' and not (value == 'Directory'):
  276.                 self.warnings.append("File extension is .directory, but Type is '%s'" % value)
  277.             elif self.fileExtension == '.desktop' and value == 'Directory':
  278.                 self.warnings.append('Files with Type=Directory should have the extension .directory')
  279.             
  280.         elif key == 'Version':
  281.             self.checkValue(key, value, type = 'number')
  282.         elif key == 'Encoding':
  283.             if value == 'Legacy-Mixed':
  284.                 self.errors.append('Encoding=Legacy-Mixed is deprecated and not supported by this parser')
  285.             elif not value == 'UTF-8':
  286.                 self.errors.append("Value of key 'Encoding' must be UTF-8")
  287.             
  288.         elif re.match('^Name' + xdg.Locale.regex + '$', key):
  289.             pass
  290.         elif re.match('^GenericName' + xdg.Locale.regex + '$', key):
  291.             pass
  292.         elif re.match('^Comment' + xdg.Locale.regex + '$', key):
  293.             pass
  294.         elif key == 'NoDisplay':
  295.             self.checkValue(key, value, type = 'boolean')
  296.         elif key == 'Hidden':
  297.             self.checkValue(key, value, type = 'boolean')
  298.         elif key == 'Terminal':
  299.             self.checkValue(key, value, type = 'boolean')
  300.             self.checkType(key, 'Application')
  301.         elif key == 'TryExec':
  302.             self.checkValue(key, value)
  303.             self.checkType(key, 'Application')
  304.         elif key == 'Exec':
  305.             self.checkValue(key, value)
  306.             self.checkType(key, 'Application')
  307.         elif key == 'Path':
  308.             self.checkValue(key, value)
  309.             self.checkType(key, 'Application')
  310.         elif re.match('^Icon' + xdg.Locale.regex + '$', key):
  311.             self.checkValue(key, value)
  312.         elif re.match('^SwallowTitle' + xdg.Locale.regex + '$', key):
  313.             self.checkType(key, 'Application')
  314.         elif key == 'SwallowExec':
  315.             self.checkValue(key, value)
  316.             self.checkType(key, 'Application')
  317.         elif key == 'FilePatterns':
  318.             self.checkValue(key, value, type = 'regex', list = True)
  319.             self.checkType(key, 'Application')
  320.         elif key == 'Actions':
  321.             self.checkValue(key, value, list = True)
  322.             self.checkType(key, 'Application')
  323.         elif key == 'MimeType':
  324.             self.checkValue(key, value, type = 'regex', list = True)
  325.             self.checkType(key, 'Application')
  326.         elif key == 'Categories':
  327.             self.checkValue(key, value)
  328.             self.checkType(key, 'Application')
  329.             self.checkCategorie(value)
  330.         elif key == 'OnlyShowIn':
  331.             self.checkValue(key, value, list = True)
  332.             self.checkOnlyShowIn(value)
  333.         elif key == 'NotShowIn':
  334.             self.checkValue(key, value, list = True)
  335.             self.checkOnlyShowIn(value)
  336.         elif key == 'StartupNotify':
  337.             self.checkValue(key, value, type = 'boolean')
  338.             self.checkType(key, 'Application')
  339.         elif key == 'StartupWMClass':
  340.             self.checkType(key, 'Application')
  341.         elif key == 'SortOrder':
  342.             self.checkValue(key, value, list = True)
  343.             self.checkType(key, 'Directory')
  344.         elif key == 'URL':
  345.             self.checkValue(key, value)
  346.             self.checkType(key, 'URL')
  347.         elif key == 'Dev':
  348.             self.checkValue(key, value)
  349.             self.checkType(key, 'FSDevice')
  350.         elif key == 'FSType':
  351.             self.checkValue(key, value)
  352.             self.checkType(key, 'FSDevice')
  353.         elif key == 'MountPoint':
  354.             self.checkValue(key, value)
  355.             self.checkType(key, 'FSDevice')
  356.         elif re.match('^UnmountIcon' + xdg.Locale.regex + '$', key):
  357.             self.checkValue(key, value)
  358.             self.checkType(key, 'FSDevice')
  359.         elif key == 'ReadOnly':
  360.             self.checkValue(key, value, type = 'boolean')
  361.             self.checkType(key, 'FSDevice')
  362.         elif key == 'ServiceTypes':
  363.             self.checkValue(key, value, list = True)
  364.             self.warnings.append("Key '%s' is a KDE extension" % key)
  365.         elif key == 'DocPath':
  366.             self.checkValue(key, value)
  367.             self.warnings.append("Key '%s' is a KDE extension" % key)
  368.         elif re.match('^Keywords' + xdg.Locale.regex + '$', key):
  369.             self.checkValue(key, value, list = True)
  370.             self.warnings.append("Key '%s' is a KDE extension" % key)
  371.         elif key == 'InitialPreference':
  372.             self.checkValue(key, value, type = 'number')
  373.             self.warnings.append("Key '%s' is a KDE extension" % key)
  374.         elif re.match('^MiniIcon' + xdg.Locale.regex + '$', key):
  375.             self.checkValue(key, value)
  376.             self.warnings.append("Key '%s' is deprecated" % key)
  377.         elif key == 'TerminalOptions':
  378.             self.checkValue(key, value)
  379.             self.warnings.append("Key '%s' is deprecated" % key)
  380.         elif key == 'DefaultApp':
  381.             self.checkValue(key, value)
  382.             self.warnings.append("Key '%s' is deprecated" % key)
  383.         elif key == 'Protocols':
  384.             self.checkValue(key, value, list = True)
  385.             self.warnings.append("Key '%s' is deprecated" % key)
  386.         elif key == 'Extensions':
  387.             self.checkValue(key, value, list = True)
  388.             self.warnings.append("Key '%s' is deprecated" % key)
  389.         elif key == 'BinaryPattern':
  390.             self.checkValue(key, value)
  391.             self.warnings.append("Key '%s' is deprecated" % key)
  392.         elif key == 'MapNotify':
  393.             self.checkValue(key, value)
  394.             self.warnings.append("Key '%s' is deprecated" % key)
  395.         elif re.match('^X-[a-zA-Z0-9-]+', key):
  396.             pass
  397.         else:
  398.             self.errors.append('Invalid key: %s' % key)
  399.  
  400.     
  401.     def checkType(self, key, type):
  402.         if not self.type == type:
  403.             self.errors.append("Key '%s' only allowed in Type=%s" % (key, type))
  404.         
  405.  
  406.     
  407.     def checkOnlyShowIn(self, value):
  408.         values = self.getList(value)
  409.         valid = [
  410.             'GNOME',
  411.             'KDE',
  412.             'ROX',
  413.             'XFCE',
  414.             'Old']
  415.         for item in values:
  416.             if item not in valid:
  417.                 self.errors.append("'%s' is not a registered OnlyShowIn value" % item)
  418.                 continue
  419.         
  420.  
  421.     
  422.     def checkCategorie(self, value):
  423.         values = self.getList(value)
  424.         valid = [
  425.             'Legacy',
  426.             'Core',
  427.             'Development',
  428.             'Building',
  429.             'Debugger',
  430.             'IDE',
  431.             'GUIDesigner',
  432.             'Profiling',
  433.             'RevisionControl',
  434.             'Translation',
  435.             'Office',
  436.             'Calendar',
  437.             'ContactManagement',
  438.             'Database',
  439.             'Dictionary',
  440.             'Chart',
  441.             'Email',
  442.             'Finance',
  443.             'FlowChart',
  444.             'PDA',
  445.             'ProjectManagement',
  446.             'Presentation',
  447.             'Spreadsheet',
  448.             'WordProcessor',
  449.             'Graphics',
  450.             '2DGraphics',
  451.             'VectorGraphics',
  452.             'RasterGraphics',
  453.             '3DGraphics',
  454.             'Scanning',
  455.             'OCR',
  456.             'Photograph',
  457.             'Viewer',
  458.             'Settings',
  459.             'DesktopSettings',
  460.             'HardwareSettings',
  461.             'PackageManager',
  462.             'Network',
  463.             'Dialup',
  464.             'InstantMessaging',
  465.             'IRCClient',
  466.             'FileTransfer',
  467.             'HamRadio',
  468.             'News',
  469.             'P2P',
  470.             'RemoteAccess',
  471.             'Telephony',
  472.             'WebBrowser',
  473.             'WebDevelopment',
  474.             'AudioVideo',
  475.             'Audio',
  476.             'Midi',
  477.             'Mixer',
  478.             'Sequencer',
  479.             'Tuner',
  480.             'Video',
  481.             'TV',
  482.             'AudioVideoEditing',
  483.             'Player',
  484.             'Recorder',
  485.             'DiscBurning',
  486.             'Game',
  487.             'ActionGame',
  488.             'AdventureGame',
  489.             'ArcadeGame',
  490.             'BoardGame',
  491.             'BlocksGame',
  492.             'CardGame',
  493.             'KidsGame',
  494.             'LogicGame',
  495.             'RolePlaying',
  496.             'Simulation',
  497.             'SportsGame',
  498.             'StrategyGame',
  499.             'Education',
  500.             'Art',
  501.             'Art',
  502.             'Contruction',
  503.             'Music',
  504.             'Languages',
  505.             'Science',
  506.             'Astronomy',
  507.             'Biology',
  508.             'Chemistry',
  509.             'Geology',
  510.             'Math',
  511.             'MedicalSoftware',
  512.             'Physics',
  513.             'Teaching',
  514.             'Amusement',
  515.             'Applet',
  516.             'Archiving',
  517.             'Electronics',
  518.             'Emulator',
  519.             'Engineering',
  520.             'FileManager',
  521.             'Shell',
  522.             'Screensaver',
  523.             'TerminalEmulator',
  524.             'TrayIcon',
  525.             'System',
  526.             'Filesystem',
  527.             'Monitor',
  528.             'Security',
  529.             'Utility',
  530.             'Accessibility',
  531.             'Calculator',
  532.             'Clock',
  533.             'TextEditor',
  534.             'KDE',
  535.             'GNOME',
  536.             'GTK',
  537.             'Qt',
  538.             'Motif',
  539.             'Java',
  540.             'ConsoleOnly']
  541.         for item in values:
  542.             if item not in valid:
  543.                 self.errors.append("'%s' is not a registered Category" % item)
  544.             
  545.         
  546.  
  547.  
  548.